package com.cwz.controller;

import com.cwz.entity.Material;
import com.cwz.entity.User;
import com.cwz.exception.ErrorEnum;
import com.cwz.param.ApiHttpQueryParam;
import com.cwz.param.UserQueryParam;
import com.cwz.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
@CrossOrigin
public class UserController {


    @Resource
    private UserService userService;

    @PostMapping("/login")
    public Map<String, Object> login(User user, HttpSession session){

        log.info("用户登录:{}",user);
       return userService.login(user,session);

    }


    @PostMapping("/save")
    public Map<String,Object> save(User user,HttpSession session){
        log.info("用户新增:{}",user);
        return userService.save(user,session);
    }

    @GetMapping("/page")
    public Map<String,Object> query(UserQueryParam param, HttpSession session){
        return  userService.page(param,session);

    }

    @PostMapping("/disable")
    public Map<String, Object> disable(User user, HttpSession session) {
        return userService.disable(user, session);
    }

    @GetMapping("/session")
    public Map<String , Object> heartBeat(){
        Map<String,Object> map=new HashMap<>();
        map.put("code",ErrorEnum.SUCCESS.getCode());
        map.put("msg",ErrorEnum.SUCCESS.getMsg());
        return map;
    }

    @PostMapping("/register")
    public Map<String,Object> registerUser(@RequestBody User user) {
        try {
            return userService.register(user);
        } catch (Exception e) {
            log.info("注册失败");
            throw new RuntimeException(e);
        }
    }


}
